package com.lee.dutylower.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lee.dutylower.BaseActivity;
import com.lee.dutylower.MyApplication;
import com.lee.dutylower.R;
import com.lee.dutylower.db.greenEntity.PostRecord;
import com.lee.dutylower.db.greenEntity.Squad;
import com.lee.dutylower.db.greenEntity.SquadRecord;
import com.lee.dutylower.db.greendao.PostRecordDao;
import com.lee.dutylower.db.greendao.SquadRecordDao;
import com.lee.dutylower.domain.SquadTypeEnum;
import com.lee.dutylower.task.NScheduleTask;
import com.lee.dutylower.task.ScheduleTask;
import com.lee.dutylower.utils.Globals;
import com.lee.dutylower.utils.SharedPreferencesUtils;
import com.lee.dutylower.utils.ToastUtils;
import com.lee.dutylower.view.scheduling.OneDayScheduleFragment;
import com.lee.dutylower.view.wiget.ScheduleView;

import java.util.ArrayList;
import java.util.List;

public class SchedulingActivity extends BaseActivity {
    private ScheduleView onSchedule;
    private ScheduleView dutySchedule;
    private ScheduleView restSchedule;
    private SquadRecordDao squadRecordDao;
    private PostRecordDao postRecordDao;
    private List<SquadRecord> onValues;
    private List<SquadRecord> dutyValues;
    private List<SquadRecord> restValues;

    //private OneDayScheduleFragment[] oneDayScheduleFragments;
    private ListView lvWeek;
    private Button btnStart;
    private String[] weeks = new String[]{"周一值班表","周二值班表","周三值班表","周四值班表","周五值班表","周六值班表","周日值班表"};

    //派位表集合
    private ArrayList<PostRecord> postRecordDBs;

    private int dayofweek = 1;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 0:
                    if (dayofweek <= 7){
                        dayofweek++;
                        NScheduleTask task = new NScheduleTask(SchedulingActivity.this, handler, dayofweek);
                        task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
                    }
                    break;
                case 1:
                    btnStart.setVisibility(View.INVISIBLE);
                    lvWeek.setVisibility(View.VISIBLE);
                    SharedPreferencesUtils.setParam(getApplicationContext(),"scheduled",true);
//                    oneDayScheduleFragment.setDayofweek(5);
//                    llReplace.setVisibility(View.VISIBLE);
//                    getFragmentManager().beginTransaction().add(R.id.ll_replace,oneDayScheduleFragment, "onday").commit();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scheduling);

        initView();

        //oneDayScheduleFragments = new OneDayScheduleFragment[7];
    }

    private void initView() {
        squadRecordDao = MyApplication.getApplication().getDaoSession().getSquadRecordDao();
        postRecordDao = MyApplication.getApplication().getDaoSession().getPostRecordDao();
        onSchedule = (ScheduleView) findViewById(R.id.sv_squad_on);
        dutySchedule = (ScheduleView) findViewById(R.id.sv_squad_duty);
        restSchedule = (ScheduleView) findViewById(R.id.sv_squad_rest);

        lvWeek = (ListView) findViewById(R.id.lv_week);
        btnStart = (Button) findViewById(R.id.btn_start);
        lvWeek.setVisibility(View.INVISIBLE);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(),R.layout.listview_week_item,R.id.tv_week,weeks);
        lvWeek.setAdapter(adapter);
        lvWeek.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getApplicationContext(),OneDayShowActivity.class);
                intent.putExtra("week",position+1);
                startActivity(intent);
            }
        });

        onSchedule.setItem(SquadTypeEnum.OnSquad.getName());
        dutySchedule.setItem(SquadTypeEnum.DutySquad.getName());
        restSchedule.setItem(SquadTypeEnum.RestSquad.getName());
        squadRecordDao.queryBuilder().where(SquadRecordDao.Properties.SquadType.eq(SquadTypeEnum.OnSquad.getType())).build().list();
        onValues = squadRecordDao.queryBuilder()
                .where(SquadRecordDao.Properties.SquadType.eq(SquadTypeEnum.OnSquad.getType()))
                .build().list();
        dutyValues = squadRecordDao.queryBuilder()
                .where(SquadRecordDao.Properties.SquadType.eq(SquadTypeEnum.DutySquad.getType()))
                .build().list();
        restValues = squadRecordDao.queryBuilder()
                .where(SquadRecordDao.Properties.SquadType.eq(SquadTypeEnum.RestSquad.getType()))
                .build().list();


        if (onValues == null || onValues.size() == 0){
            onSchedule.updateTextInfo(false,"无上岗班");
            onSchedule.updateList(null);
        }else {
            ArrayList<Squad> on = new ArrayList<>();
            for (SquadRecord onValue : onValues) {
                on.add(onValue.getSquad());
            }
            Globals.ON.addAll(on);
            onSchedule.updateList(on);
        }

        if (dutyValues == null || dutyValues.size() == 0){
            dutySchedule.updateTextInfo(false,"无值班班");
            dutySchedule.updateList(null);
        }else {
            ArrayList<Squad> duty = new ArrayList<>();
            for (SquadRecord dutyValue : dutyValues) {
                duty.add(dutyValue.getSquad());
            }
            Globals.DUTY.addAll(duty);
            dutySchedule.updateList(duty);
        }

        if (restValues == null || restValues.size() == 0){
            restSchedule.updateTextInfo(false,"无补岗班");
            restSchedule.updateList(null);
        }else {
            ArrayList<Squad> rest = new ArrayList<>();
            for (SquadRecord restValue : restValues) {
                rest.add(restValue.getSquad());
            }
            Globals.REST.addAll(rest);
            restSchedule.updateList(rest);
        }

        boolean scheduled = (boolean) SharedPreferencesUtils.getParam(getApplicationContext(),"scheduled",false);
        if (scheduled){
            handler.sendEmptyMessage(1);
        }
    }

    public void reSet(View view) {
        SharedPreferencesUtils.setParam(getApplicationContext(),"scheduled",false);
        squadRecordDao.deleteAll();
        postRecordDao.deleteAll();
        initView();
        Globals.ON.clear();
        Globals.REST.clear();
        Globals.DUTY.clear();
        lvWeek.setVisibility(View.INVISIBLE);
        btnStart.setVisibility(View.VISIBLE);
    }

    public void Scheduling(View view) {
        boolean scheduled = (boolean) SharedPreferencesUtils.getParam(getApplicationContext(),"scheduled",false);
        if (scheduled){
            handler.sendEmptyMessage(1);
        }else {
            onValues = squadRecordDao.queryBuilder()
                    .where(SquadRecordDao.Properties.SquadType.eq(SquadTypeEnum.OnSquad.getType()))
                    .build().list();
            dutyValues = squadRecordDao.queryBuilder()
                    .where(SquadRecordDao.Properties.SquadType.eq(SquadTypeEnum.DutySquad.getType()))
                    .build().list();
            restValues = squadRecordDao.queryBuilder()
                    .where(SquadRecordDao.Properties.SquadType.eq(SquadTypeEnum.RestSquad.getType()))
                    .build().list();

            if (onValues==null || onValues.size()!=3){
                ToastUtils.showLong(this,"上岗班数不合法");
                return;
            }
            if (dutyValues==null || dutyValues.size()!=1){
                ToastUtils.showLong(this,"值班班数不合法");
                return;
            }
//            if (restValues==null || restValues.size()!=1){
//                ToastUtils.showLong(this,"补岗班数不合法");
//                return;
//            }

            NScheduleTask task = new NScheduleTask(this,handler,dayofweek);
            task.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
        }
    }
}
